<?php
/**
 * Plugin to support conditional GET for php pages (using ETag)
 * Should be loaded the very last in the plugins stack
 * Note: Apache 2.2.8 up to "unknown" version adds -gzip outside Etag
 * (we are not handling the apache bug here)
 *
 * @author $Author: danila $
 * @version $Id: Conditional.php 101 2010-03-20 01:35:29Z hami $
 *
 */
class Sfs_Http_Conditional extends Zend_Controller_Plugin_Abstract
{

	public function dispatchLoopShutdown()
	{


		$send_body = true;

		$etag = '"' . md5($this->getResponse()->getBody()) . '"';
		$this->getResponse()->setHeader("ETag", $etag, true);

		$inm = explode(',', getenv("HTTP_IF_NONE_MATCH"));
		foreach ($inm as $i) {
			if (trim($i) == $etag) {
				$this->getResponse()->clearAllHeaders()
				->setHttpResponseCode(304)->clearBody();
				$send_body = false;
				break;
			}
		}


		if ($send_body) {
			$this->getResponse()
			->setRawHeader("Cache-Control: max-age=7200, must-revalidate, no-cache")
			->setRawHeader("Expires: ". gmdate('D, d M Y H:i:s', time() + 7200) . ' GMT')
			->setHeader("Content-Length", strlen($this->getResponse()->getBody()));
		}
	}
}